package com.example.registration.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

/**
 * 图标格式转换， byte，bitmap 和base64
 */
public class ImageHelper {

    /**
     * 将图片转成byte数组
     *
     * @param bitmap 图片
     * @return 图片的字节数组
     */
    public static byte[] bitmap2Byte(Bitmap bitmap) {
        if (null == bitmap) throw new NullPointerException();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        return outputStream.toByteArray();
    }

    /**
     * 将byte转base64字符串
     *
     * @param imageByte 图片
     * @return Base64 String
     */
    public static String byte2Base64(byte[] imageByte) {
        if (null == imageByte) return null;
        return Base64.encodeToString(imageByte, Base64.DEFAULT);
    }


    /**
     * Base64转Bitmap
     *
     * @param base64String base64数据流
     * @return Bitmap 图片
     */
    public static Bitmap base642Bitmap(String base64String) {
        if (null == base64String) throw new NullPointerException();
        byte[] decode = Base64.decode(base64String.split(",")[1], Base64.DEFAULT);
        Bitmap mBitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
        return mBitmap;
    }
}
